package com.edol.painting.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.edol.painting.R;
import com.edol.painting.painting.Brush;

public class BrushPickerContainerView extends LinearLayout implements View.OnClickListener{

    private static final String TAG = "BrushPickerContainerView";
    private static final int SELECT_BG_IDX = 1;

    private ImageView mPenView = null;
    private ImageView mPencilView = null;
    private ImageView mCharcoalView = null;

    private ImageView mWaterColorView = null;
    private ImageView mMarkerView = null;
    private ImageView mChalkView = null;

    private int mSelect = R.id.brush_pen;

    public BrushPickerContainerView(Context context) {
        super(context);
    }

    public BrushPickerContainerView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public BrushPickerContainerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mPenView = (ImageView)findViewById(R.id.brush_pen);
        mPencilView = (ImageView)findViewById(R.id.brush_pencil);
        mCharcoalView = (ImageView)findViewById(R.id.brush_soft_charcoal);

        mWaterColorView = (ImageView)findViewById(R.id.brush_watercolor);
        mMarkerView = (ImageView)findViewById(R.id.brush_marker);
        mChalkView = (ImageView)findViewById(R.id.brush_chalk);

        setOnItemClickListener(this);

        updateItemView(mSelect);
    }

    private void setOnItemClickListener(OnClickListener listener) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child != null && child instanceof ImageView) {
                child.setOnClickListener(listener);
            }
        }
    }

    public interface OnBrushTypeChangedListener {
        void brushTypeChanged(int brushType);
    }
    private OnBrushTypeChangedListener mBrushTypeChangedListener = null;
    public void setBrushTypeChangeListener(OnBrushTypeChangedListener listener) {
        mBrushTypeChangedListener = listener;
    }

    private void updateItemView(int id) {
        mPenView.setImageResource(R.drawable.ic_menu_pen);
        mPencilView.setImageResource(R.drawable.ic_menu_pencil);
        mCharcoalView.setImageResource(R.drawable.ic_menu_soft_charcoal);
        mWaterColorView.setImageResource(R.drawable.ic_menu_watercolor);
        mMarkerView.setImageResource(R.drawable.ic_menu_marker);
        mChalkView.setImageResource(R.drawable.ic_menu_chalk);

        switch(id) {
            case R.id.brush_pen:
                mPenView.setImageResource(R.drawable.ic_menu_pen_selected);
                break;
            case R.id.brush_pencil:
                mPencilView.setImageResource(R.drawable.ic_menu_pencil_selected);
                break;
            case R.id.brush_soft_charcoal:
                mCharcoalView.setImageResource(R.drawable.ic_menu_soft_charcoal_selected);
                break;
            case R.id.brush_watercolor:
                mWaterColorView.setImageResource(R.drawable.ic_menu_watercolor_selected);
                break;
            case R.id.brush_marker:
                mMarkerView.setImageResource(R.drawable.ic_menu_marker_selected);
                break;
            case R.id.brush_chalk:
                mChalkView.setImageResource(R.drawable.ic_menu_chalk_selected);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int resID = v.getId();
        if (resID == mSelect) {
            return;
        }
        mSelect = resID;
        int brushType = Brush.BRUSH_PEN;
        switch(resID) {
            case R.id.brush_pen:
                brushType = Brush.BRUSH_PEN;
                break;
            case R.id.brush_pencil:
                brushType = Brush.BRUSH_PENCIL;
                break;
            case R.id.brush_soft_charcoal:
                brushType = Brush.BRUSH_SOFT_CHARCOAL;
                break;
            case R.id.brush_watercolor:
                brushType = Brush.BRUSH_WATERCOLOR;
                break;
            case R.id.brush_marker:
                brushType = Brush.BRUSH_MARKER;
                break;
            case R.id.brush_chalk:
                brushType = Brush.BRUSH_CHALK;
                break;
        }
        updateItemView(resID);
        mBrushTypeChangedListener.brushTypeChanged(brushType);
    }

    public void setBrushType(int mBrushType) {
        int brush = mBrushType;
        int viewID = -1;
        switch(brush) {
            case Brush.BRUSH_PEN:
                viewID = R.id.brush_pen;
                break;
            case Brush.BRUSH_PENCIL:
                viewID = R.id.brush_pencil;
                break;
            case Brush.BRUSH_SOFT_CHARCOAL:
                viewID = R.id.brush_soft_charcoal;
                break;
            case Brush.BRUSH_WATERCOLOR:
                viewID = R.id.brush_watercolor;
                break;
            case Brush.BRUSH_MARKER:
                viewID = R.id.brush_marker;
                break;
            case Brush.BRUSH_CHALK:
                viewID = R.id.brush_chalk;
                break;
        }
        if (viewID != -1 && viewID != mSelect) {
            mSelect = viewID;
            updateItemView(mSelect);
        }
    }

}
